package com.example.security.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

	@Autowired
	private UserDetailsService customUserDetailsService;
	@Autowired
	private AuthenticationSuccessHandler authenticationSuccessHandler;
	@Autowired
	private AuthenticationFailureHandler authenticationFailureHandler;
	
	@Bean
	public PasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable()
			.httpBasic()
			.and()
			.formLogin()
			.loginPage("/auth/login")
			.loginProcessingUrl("/login")
//			.defaultSuccessUrl("/main/common")
			.successHandler(authenticationSuccessHandler)
//			.failureUrl("/auth/login?error=true")
			.failureHandler(authenticationFailureHandler)
			.and()
			.logout()
			.logoutUrl("/auth/logout")
			.logoutSuccessUrl("/auth/login")
			.and()
			.authorizeRequests()
			.antMatchers("/fonts/**", "/js/**").permitAll() // 默认不拦截静态资源的url
			.antMatchers("/auth/**").permitAll()
			.antMatchers("/admin/**").hasRole("ADMIN")
			.antMatchers("/user/**").hasRole("USER")
			.anyRequest().authenticated();
	}

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		// db user test
		auth.userDetailsService(customUserDetailsService)// 设置自定义的userDetailsService
			.passwordEncoder(passwordEncoder());
		// in memory test
//        auth.inMemoryAuthentication()
//            .withUser("admin").password("{noop}adminpass").roles("ADMIN", "USER") // 管理员，同时具有 ADMIN,USER权限，可以访问所有资源
//            .and()
//            .withUser("spring").password("{noop}123456").roles("USER"); // 普通用户，只能访问有限的资源。
	}
}
